package com.wang.decagency.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.decagency.entity.SysBase;
import com.wang.decagency.entity.SysUser;
import com.wang.decagency.service.impl.SysUserServiceImpl;
import com.wang.utils.ResultEntity;
import com.wang.utils.TokenUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-05-29
 */
@RestController
@RequestMapping("/decagency/user")
@Slf4j
@Api(tags = "用户管理")
public class SysUserController {

    @Autowired
    private SysUserServiceImpl SysUserService;

    @PostMapping("/getSysUserList")
    @ApiOperation(value = "查询用户列表", notes = "查询用户列表")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功"), @ApiResponse(code = 404, message = "用户不存在")})
    public Object getUsers(@RequestBody Map<String, Object> mapParam) throws InvocationTargetException, IllegalAccessException {
        SysUser sysUser = JSON.parseObject(JSON.toJSONString(mapParam), SysUser.class);
        log.info("sysUser:" + JSON.toJSONString(sysUser));
        // 从params中获取分页参数
        int pageNum = mapParam.get("pageNum") != null ? (int) mapParam.get("pageNum") : 1;
        int pageSize = mapParam.get("pageSize") != null ? (int) mapParam.get("pageSize") : 5;
        log.info(JSON.toJSONString("pageNum:" + pageNum));
        // 创建QueryWrapper对象
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        // 添加不等于条件
        queryWrapper.ne("user_name", "admin");
        // 添加like条件
        if (sysUser.getUserName() != null) {
            queryWrapper.like("user_name", sysUser.getUserName());
        }
        queryWrapper.orderByDesc("user_id");
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        IPage<SysUser> SysUserList = SysUserService.page(page, queryWrapper);
        return ResultEntity.ok(SysUserList);
    }


    @PostMapping("/getByKey")
    @ApiOperation(value = "查询详情", notes = "查询详情")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object getByKey(@RequestBody @ApiParam(name = "cost", value = "报关单信息") SysUser obj) throws Exception {
        obj = SysUserService.getById(obj.getUserId());
        return ResultEntity.ok(obj);
    }


    @PostMapping({"/addSysUser","/editSysUser"})
    @ApiOperation(value = "添加用户", notes = "添加一个新的用户")
    public Object addSysUser(@RequestBody @ApiParam(name = "SysUser", value = "用户信息", required = true) SysUser SysUser) {
        if (Objects.isNull(SysUser) || SysUser.getPassWord() == null || SysUser.getUserName() == null) {
            return ResultEntity.fail("入参不全");
        }
        SysUser.setPassWord(DigestUtils.md5Hex(SysUser.getPassWord()));
        SysUserService.saveOrUpdate(SysUser);
        return ResultEntity.ok();
    }


    @PostMapping({"/delSysUser", "delByKeys"})
    @ApiOperation(value = "删除多个用户", notes = "根据用户id列表删除多个用户")
    public Object deleteSysUsers(@RequestBody List<Integer> ids) {
        SysUserService.removeByIds(ids);
        return ResultEntity.ok();
    }

    @PostMapping("/login")
    @ApiOperation(value = "添加用户", notes = "添加一个新的用户")
    public Object login(@RequestBody @ApiParam(name = "SysUser", value = "用户信息", required = true) SysUser SysUser) {
        log.info(JSON.toJSONString(SysUser));
        if (Objects.isNull(SysUser) || SysUser.getPassWord() == null || SysUser.getUserName() == null) {
            return ResultEntity.fail("入参不全");
        }
        SysUser.setPassWord(DigestUtils.md5Hex(SysUser.getPassWord()));

        SysUser = SysUserService.getOne(new QueryWrapper<>(SysUser));
        if (SysUser == null || SysUser.getUserName() == null) {
            return ResultEntity.fail("登录验证失败");
        }
        String token = new TokenUtil().generToken(SysUser);
        log.info("token:" + token);
        return ResultEntity.ok(token);
    }

}
